import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StackLayout;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class Principal {

	public Principal(final Shell shell) {

		final StackLayout stackLayout = new StackLayout();
		shell.setLayout(stackLayout);

		final Button b1 = new Button(shell, SWT.PUSH);
		b1.setText("Botao 1");
		b1.addSelectionListener(s);

		stackLayout.topControl = b1;
		shell.layout();

	}
	
	SelectionListener s = new SelectionListener() {
		
		@Override
		public void widgetSelected(SelectionEvent e) {
			Button b = (Button)e.widget;
			Composite c = b.getParent();
			b.getText();
			
			System.out.println(c.getChildren().length);
			
			Button prox = new Button(c, SWT.PUSH);
			prox.setText("Botao " + (Integer.parseInt(""+b.getText().charAt(b.getText().length()-1)) + 1) );
			prox.addSelectionListener(s);
			
			b.dispose();
			
			StackLayout sl = (StackLayout)c.getLayout();
			
			sl.topControl = prox;
			c.layout();
		}
		
		@Override
		public void widgetDefaultSelected(SelectionEvent e) {
		}
	};

	public static void main(String[] args) {
		Display display = new Display(); 
		Shell shell = new Shell(display);

		Principal p = new Principal(shell);

		shell.setSize(100, 100);
		shell.open();

		while(!shell.isDisposed()){
			if(display.readAndDispatch()){
				display.sleep();
			}
		}
		display.dispose();
	}
}
